<template>
  <el-dialog
    :title="type==='1'?'增加用户':'修改用户'"
    width="50%"
    :visible.sync="visible"
    @close="$emit('update:show', false)"
    :show="show"
  >
    <el-form  :model="formInline"  label-width="80px"  label-position="left" :rules="rules" ref="updateUser">
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="角色号" prop="name">
            <el-input v-model="formInline.name" placeholder="角色号"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="角色名称" prop="account">
            <el-input v-model="formInline.account" placeholder="角色名称"></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input type="textarea" v-model="formInline.remark"></el-input>
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
    <el-button @click="close('updateUser')">取 消</el-button>
    <el-button type="primary" @click="sure('updateUser')">确 定</el-button>
  </span>
  </el-dialog>
</template>

<script>
  import ShowMadal from '@/mixins/showmadal'
  export default {
    name: 'Updateuser',
    mixins: [ShowMadal],
    props: {
      row: {
        type: Object,
        default: {}
      },
      type: {
        type: String,
        default: '1'
      }
    },
    watch: {
      row: function() {
        this.formInline = {
          name: this.row.name || '',
          account: this.row.account,
          telephone: this.row.telephone || '',
          cellphone: this.row.cellphone || '',
          email: this.row.email || '',
          group: this.row.group ? String(this.row.group) :'1',
          userId: this.row.userId,
          remark: this.row.remark || ''
        }
      }
    },
    data: function() {
      var checkPhone = (rule, value, callback) => {
        const cellphones = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})|(19[0-9]{9})|(16[0-9]{9})$/
        if (!cellphones.test(value)) {
          return callback(new Error('请填写正确的手机号'))
        }else {
          callback()
        }
      }
      return {
        formInline: {},
        rules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' }
          ],
          account: [
            { required: true, message: '请输入登录账号', trigger: 'blur' }
          ],
          cellphone: [
            { required: true, message: '请输入手机号', trigger: 'blur' },
            { type: 'number', validator: checkPhone, trigger: 'blur' }
          ],
          group: [
            { required: true, message: '请选择分组', trigger: 'blur' }
          ],
          email: [
            { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur'] }
          ]
        }
      }
    },
    methods: {
      sure(formName) {
        this.$emit('submit', {formName: this.$refs[formName], formInline: this.formInline})
      },
      close(formName) {
        this.$emit('close', this.$refs[formName])
      }
    }
  }
</script>

<style scoped>

</style>
